package com.lin.csdn.controller;

import com.lin.csdn.model.po.Messege;
import com.lin.csdn.service.impl.PicReportService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/ReportServlet")
public class ReportServlet extends HttpServlet {
    /**
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     *
     * 0:新增举报 AddReport
     * 1：读取举报信息  GetReport
     * 2：读取举报照片截图  GetReportPicUrl
     * 3：封禁指定的账号以及所有的博客 BanUserAll
     */
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding ("UTF-8");
        resp.setContentType("application/json;charset=UTF-8");
        //建立功能代表的code调用
        String AddReport="0";
        String GetReport="1";
        String GetReportPicUrl="2";
        String BanUserAll="3";
        //获取req数据
        String code=req.getParameter("code");

        List<Object> data;
        PicReportService service=new PicReportService();
        PrintWriter writer=resp.getWriter();
        JSONArray array=new JSONArray();

        //根据需求调用各自的方法
        if(code.equals(AddReport)){
            JSONObject object=new JSONObject();
            String blogId=req.getParameter("blogId");
            String context=req.getParameter("context");

            int reportId=service.addReport(context,blogId);
            object.put("reportId",reportId);
            writer.print(object);
        }
        else if(code.equals(GetReport)){
            data=service.search();
            for(int i=0;i<data.size();i++){
                JSONObject object=new JSONObject();
                object.put("id",data.get(i));
                i++;
                object.put("context", data.get(i));
                i++;
                object.put("blogId",data.get(i));
                i++;
                object.put("date",data.get(i));
                array.put(object);
            }
            writer.print(array);
        }
        else if(code.equals(GetReportPicUrl)){
            String reportId=req.getParameter("reportId");
            JSONObject object=new JSONObject();
            String result =service.reportPic(reportId);
            object.put("picurl", result);
            writer.print(object);
        }
        else if(code.equals(BanUserAll)){
            String reportId=req.getParameter("reportId");
            JSONObject object=new JSONObject();
            Messege messege=service.ban(reportId);
            object.put("result",messege.getMessage());
            writer.print(object);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
